package com.sjh.lib_app.router

import android.content.Context
import android.net.Uri
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.service.PretreatmentService
import com.sjh.baseutils.loge
import com.sjh.baseutils.println

/**
 * 路由地址预处理
 * @author Extends
 * @date 2020/6/11
 */
@Route(path = "/app/pretreatmentServiceImpl")
class PretreatmentServiceImpl : PretreatmentService {
    override fun onPretreatment(context: Context?, postcard: Postcard?): Boolean {

//        Thread.currentThread().stackTrace.forEach {
//            "${it.className}(${it.methodName})[${it.lineNumber}]".println()
//        }

        if(postcard?.path?.startsWith("http") == true){
            "当前是跳转地址是：${postcard.path},需要接管".loge()
            //todo 后续加上webActivity的前缀，重新访问
            return false
        }

        val index = postcard?.path?.indexOf("?")?:-1
        if(index > 0 && postcard?.uri == null){
            "当前是跳转地址是：${postcard?.path},需要接管".loge()
            postcard?.uri = Uri.parse("${SchemeFilterActivity.getSchemeUrl()}${postcard?.path}")
            postcard?.path = postcard?.path?.substring(0,index)
//            postcard?.navigation(context)
            return true
        }
        return true
    }

    override fun init(context: Context?) {
    }
}